﻿package Albums 
{
	import com.greensock.TweenLite;
	import com.junkbyte.console.Cc;
	
	import Common.CommonMessage;
	import Common.ImageInfo;
	import Common.TextMessage;
	import Common.Room;
	import Core.Main;	
	import Core.ImageContainer;
	import Core.ImagesViewer;
	import Network.XMLLoader;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	[Event(name = "album_selected", type = "Albums.AlbumEvent")]
	public class AlbumsPage extends Sprite 
	{	
		private const MAX_ALBUMS_IN_PAGE:int = 5;
		private const PADDING:int = 5;
		
		private var FWidth:Number;
		private var FHeight:Number;
		private var FLoader:XMLLoader;
		private var FAlbumsContainer:Sprite;
		private var FAlbumsMask:Sprite;
		private var FViewer:ImagesViewer;
		private var FAlbums:Vector.<AbstractIcon>;
		private var FLeft:AlbumArrow;
		private var FRight:AlbumArrow;
		private var FPlus:AlbumCreateIcon;
		private var FRoot:Main;
		private var FSelected:AlbumIcon;
		//pagiation
		private var FCurrentID:int;
		
		public function AlbumsPage() 
		{
			FWidth = new Number();
			FHeight = new Number();
			FAlbumsContainer = new Sprite();
			FAlbumsMask = new Sprite();
			addChild(FAlbumsContainer);
			addChild(FAlbumsMask);
			FAlbumsContainer.mask = FAlbumsMask;
			FAlbums = new Vector.<AbstractIcon>;
			FLeft = new AlbumArrow(1);
			FRight = new AlbumArrow(-1);
			FPlus = new AlbumCreateIcon();			
			addChild(FLeft);
			addChild(FRight);
			FLeft.addEventListener(MouseEvent.MOUSE_DOWN, MoveLeft);
			FRight.addEventListener(MouseEvent.MOUSE_DOWN, MoveRight);
			addEventListener(MouseEvent.MOUSE_WHEEL, OnMouseWheel);
		}
		
		private function OnMouseWheel(e:MouseEvent):void 
		{		
			e.delta > 0 ? MoveRight(null) : MoveLeft(null);
		}
		
		private function MoveLeft(e:MouseEvent):void 
		{
			if (FCurrentID >= 1)
			{
				FCurrentID--;
				PlaceContainer();
			}
		}
		
		private function MoveRight(e:MouseEvent):void 
		{
			if (FCurrentID + 1 < FAlbums.length)
			{
				FCurrentID++;
				PlaceContainer();
			}
		}
		
		private function PlaceContainer():void 
		{
			var vXByID:Number;			
			if (FAlbums && FAlbums.length > 0 && FLeft.visible)
			{
				var vWidth:Number = FAlbums.length * (FHeight + PADDING);
				var vDistance:Number = vWidth -FAlbums[FCurrentID].x;				
				vXByID = -(FCurrentID * (FHeight + PADDING))  + FLeft.width;
			}
			else
			{
				FCurrentID = 0;
				vXByID = 0;
			}
			TweenLite.killTweensOf(FAlbumsContainer);
			TweenLite.to(FAlbumsContainer, 0.5, { x:vXByID } );			
		}
		
		private function Draw():void 
		{			
			graphics.clear();
			graphics.beginFill(0, 0);
			graphics.drawRect(0, 0, FWidth, FHeight);
			graphics.endFill();					
			FLeft.Height = FHeight;
			FRight.Height = FHeight;
			FRight.x = FWidth;			
		}
				
		private function OnXMLLoaded(e:Event):void 
		{
			var vAlbum:AlbumIcon;			
			for (var i in FLoader.Rooms)
			{
				var vImages:Vector.<ImageInfo> = FLoader.ImagesByRoom(FLoader.Rooms[i].ID);								
				vAlbum = new AlbumIcon();
				if (vImages.length > 0)
					vAlbum.TitleImageFromInfo = vImages[0];
				vAlbum.AlbumRoom = FLoader.Rooms[i];
				FAlbumsContainer.addChild(vAlbum);
				vAlbum.addEventListener(MouseEvent.MOUSE_DOWN, OnAlbumClick);
				vAlbum.IsRoom = FLoader.IsRoom(FLoader.Rooms[i].ID);
				if(FViewer)
					vAlbum.Viewer = FViewer;
				FAlbums.push(vAlbum);
			}
			FAlbumsContainer.addChild(FPlus);
			FAlbums.push(FPlus);
			FPlus.addEventListener(MouseEvent.MOUSE_DOWN, CreateAlbum);
			AlbumIcon(FAlbumsContainer.getChildAt(0)).IsSelected = true;
			FRoot.AlbumsTabsManager.CurrentRoom = AlbumIcon(FAlbumsContainer.getChildAt(0));
			PlaceAlbums();
		}
		
		private function OnAlbumClick(e:MouseEvent):void 
		{
			if(e.target is AlbumIcon)
				SelectedAlbum = AlbumIcon(e.target);
		}
		
		private function CreateAlbum(e:MouseEvent):void 
		{
			var vAlbum:AlbumIcon = new AlbumIcon();
			var vMessage:TextMessage = new TextMessage();			
			vMessage.Header = 'Создание номера';
			vMessage.Description = 'Идет создание номера. Пожалуйста, подождите.';
			FRoot.MessagesCore.Show(vMessage);
			vAlbum.addEventListener(AlbumEvent.ALBUM_CREATED, OnAlbumCreated);			
			vAlbum.Create(FLoader.MainID);
		}
		
		private function OnAlbumCreated(e:AlbumEvent):void 
		{			
			FAlbumsContainer.addChild(e.Album);
			FAlbumsContainer.addChild(FPlus);
			FRoot.MessagesCore.visible = false;
			FAlbums.push(e.Album);
			SelectedAlbum = e.Album;
			if(FViewer)
				e.Album.Viewer = FViewer;
			e.Album.addEventListener(MouseEvent.MOUSE_DOWN, OnAlbumClick);
			FLoader.AddRoom(e.Album.AlbumRoom);
			e.Album.IsRoom = FLoader.IsRoom(e.Album.AlbumRoom.ID);
			PlaceAlbums();
		}
		
		public function Remove(AAlbum:AlbumIcon):void 
		{
			trace('Removing ' + AAlbum.ID);
			FAlbumsContainer.removeChild(AAlbum);
			var vIndex:int = FAlbums.indexOf(AAlbum);
			FAlbums.splice(vIndex, 1);
			FLoader.DeleteRoom(AAlbum.AlbumRoom);
			FCurrentID = 0;
			CurrentAlbum = FLoader.MainID;
			PlaceAlbums();
		}
		
		private function PlaceAlbums():void
		{
			var vPadding:Number = PADDING;
			var vAlbumSize:int = FHeight;
			var vAlbum:AbstractIcon;
			var vWidth:int = FWidth;			
			if (UseArrows)
			{
				FLeft.visible = FRight.visible = true;
				FAlbumsMask.x = FLeft.width;				
				vWidth = Math.floor(FWidth - FLeft.width * 2);
			}
			else
			{				
				FLeft.visible = FRight.visible = false;
				FAlbumsMask.x = 0;
			}
			PlaceContainer();
			FAlbumsMask.graphics.clear();
			FAlbumsMask.graphics.beginFill(0, 0);
			FAlbumsMask.graphics.drawRect(0, 0, vWidth, FHeight);
			FAlbumsMask.graphics.endFill();
			for (var i:int = 0; i < FAlbumsContainer.numChildren; i++)
			{
				if (FAlbumsContainer.getChildAt(i) is AbstractIcon)
				{
					vAlbum = AbstractIcon(FAlbumsContainer.getChildAt(i));
					vAlbum.SetSize(vAlbumSize, vAlbumSize);				
					vAlbum.x = i * (vAlbumSize + vPadding);
				}
			}
		}
		
		public function ClearTitle(AID:int):void
		{
			for (var i in FAlbums)
			{
				if (FAlbums[i] is AlbumIcon)
				{
					if(AlbumIcon(FAlbums[i]).AlbumRoom.ID == AID)
					{
						AlbumIcon(FAlbums[i]).ClearTitle();
						break;
					}
				}
			}
		}		
		
		public function UpdateAlbum(AAlbum:int, ATitle:ImageContainer):void
		{
			var vAlbum:AlbumIcon;
			for (var i in FAlbums)
			{
				if (FAlbums[i] is AlbumIcon)
				{
					vAlbum = AlbumIcon(FAlbums[i]);
					if (vAlbum.ID == AAlbum)
					{
						vAlbum.TitleImageFromContainer = ATitle;
						break;
					}
				}
			}
		}
		
		public function UpdateTitle(ATitle:ImageContainer):void
		{
			var vAlbum:AlbumIcon;
			for (var i in FAlbums)
			{
				if (FAlbums[i] is AlbumIcon)
				{
					vAlbum = AlbumIcon(FAlbums[i]);
					if (vAlbum.ID == ATitle.ObjectID)
					{
						vAlbum.TitleImageFromContainer = ATitle;
						break;
					}
				}
			}
		}
		
		public function SetSize(AWidth:Number, AHeight:Number):void
		{
			FWidth = AWidth;
			FHeight = AHeight;			
			PlaceAlbums();
			Draw();
		}
		
		//{ Getters & setters
		
		private function get UseArrows():Boolean
		{
			return FAlbums.length > MAX_ALBUMS_IN_PAGE && FWidth < (FAlbums.length * (FHeight + PADDING));
		}
		
		public function get Width():Number 
		{
			return FWidth;
		}
		
		public function set Width(AWidth:Number):void 
		{
			FWidth = AWidth;
		}
		
		public function get Height():Number 
		{
			return FHeight;
		}
		
		public function set Height(AHeight:Number):void 
		{
			FHeight = AHeight;
		}
		
		public function set Viewer(AViewer:ImagesViewer):void 
		{
			FViewer = AViewer;
			for (var i:int = 0; i < FAlbums.length; i++) 
			{
				FAlbums[i].Viewer = FViewer;
			}
		}
		
		public function set XMLS(ALoader:XMLLoader):void 
		{
			FLoader = ALoader;
			FLoader.addEventListener(Event.COMPLETE, OnXMLLoaded);
		}
		
		public function set Root(ARoot:Main):void
		{
			FRoot = ARoot;
		}
		
		public function get SelectedAlbum():AlbumIcon
		{
			for (var i in FAlbums)
			{
				if (FAlbums[i] is AlbumIcon)
				{	
					if (AlbumIcon(FAlbums[i]).IsSelected)
						return AlbumIcon(FAlbums[i]);
				}
			}
			return null;
		}
		
		public function set SelectedAlbum(AIcon:AlbumIcon):void
		{
			var vIcon:AlbumIcon = AIcon;
			if (vIcon != FSelected)
			{
				FViewer.Category = vIcon.AlbumRoom.ID;
				FRoot.AlbumsTabsManager.CurrentRoom = vIcon;
				vIcon.IsSelected = true;
				FSelected = AIcon;
				dispatchEvent(new AlbumEvent(AlbumEvent.ALBUM_SELECTED, AIcon));				
			}
		}
		
		public function set CurrentAlbum(AID:int):void
		{
			for (var i in FAlbums)
			{
				if (FAlbums[i] is AlbumIcon)
				{					
					if(AlbumIcon(FAlbums[i]).AlbumRoom.ID == AID)
					{
						SelectedAlbum = AlbumIcon(FAlbums[i]);
						break;
					}
				}
			}			
		}
		
		public function get Albums():Vector.<AlbumIcon>
		{
			var vResult:Vector.<AlbumIcon> = new Vector.<AlbumIcon>;
			for (var i in FAlbums)
			{
				if (FAlbums[i] is AlbumIcon)
				{	
					if(AlbumIcon(FAlbums[i]).IsRoom)
						vResult.push(FAlbums[i]);
				}
			}
			return vResult;
		}
		
		//} endregion
		//End of getters & setters		
	}
}